package com.tan00xu.controller;

import com.tan00xu.myAnnotation.log.LogAnnotation;
import com.tan00xu.service.ArticleService;
import com.tan00xu.utils.CmdOutputInformationUtils;
import com.tan00xu.vo.Result;
import com.tan00xu.vo.params.ArticleParam;
import com.tan00xu.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//json数据交互
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 根据条件获取文件列表
     *
     * @param pageParams 分页参数（页数和条数）
     * @return Result
     */
    @PostMapping("/listArticles")
    @LogAnnotation(module = "根据条件获取文件列表",operation = "根据条件获取文件列表")
    public Result listArchivesByPageParams(@RequestBody PageParams pageParams) {
//        CmdOutputInformationUtils.error("前端参数：\n"+pageParams);
        CmdOutputInformationUtils.info("ArticleController下的listArchivesByPageParams被请求了=>根据条件获取文件列表");
        return articleService.listArchivesByPageParams(pageParams);
    }

    /**
     * 首页最热文章列表
     *
     * @return Result
     */
    @PostMapping("/hotArticles")
    public Result hotArticles() {
        int limit = 5;
        CmdOutputInformationUtils.info("ArticleController下的hotArticles被请求了=>首页最热文章列表");
        return articleService.listHotArticles(limit);
    }

    /**
     * 首页最新文章列表
     *
     * @return Result
     */
    @PostMapping("/newArticles")
    public Result newArticles() {
        int limit = 5;
        CmdOutputInformationUtils.info("ArticleController下的newArticles被请求了=>首页最新文章列表");
        return articleService.listNewArticles(limit);
    }

    /**
     * 首页文章归档
     * @return Result
     */
    @PostMapping("/listArchives")
    public Result listArchives() {
        int limit = 5;
        CmdOutputInformationUtils.info("ArticleController下的listArchives被请求了=>首页文章归档");
        return articleService.listArchives();
    }

    /**
     * 根据文章id获取文章详情
     * @param articleId 文章id
     * @return Result
     */
    @PostMapping("/view/{id}")
    public Result getArticleById(@PathVariable("id") Long articleId) {
        CmdOutputInformationUtils.info("ArticleController下的getArticleById被请求了=>获取文章详情");
        CmdOutputInformationUtils.info("articleId:" + articleId);
        Result article = articleService.getArticleById(articleId);
        CmdOutputInformationUtils.debug("article:\n" + article);
        return article;
    }


    /**
     * 发布文章
     * @param articleParam 文章参数
     * @return
     */
    @PostMapping("/publishArticle")
    public Result publishArticle(@RequestBody ArticleParam articleParam) {
        CmdOutputInformationUtils.info("ArticleController下的publishArticle被请求了=>发布文章");
        CmdOutputInformationUtils.error("获取到的文章参数articleParam:\n" + articleParam);
        return articleService.publishArticle(articleParam);
    }

    /**
     * 根据文章id删除文章
     * @param articleId 文章id
     * @param token 用户token
     * @return
     */
    @PostMapping("/deleteArticleByArticleId/{articleId}")
    public Result deleteArticleByArticleId(@PathVariable("articleId") Long articleId,@RequestHeader("Authorization") String token) {
        CmdOutputInformationUtils.info("ArticleController下的deleteArticle被请求了=>删除文章");
        CmdOutputInformationUtils.info("articleId:" + articleId);
        return articleService.deleteArticleByArticleId(articleId,token);
//        return articleService.deleteArticle(articleId);
    }


}
